﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using System.Collections;
using System.Data;
using ESRI.ArcGIS.ADF.Web;
//using GIS.utils;
using ESRI.ArcGIS.ADF.Web.Display.Graphics;
using ESRI.ArcGIS.ADF.Web.Geometry;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.DataLayer;
using Microsoft.Office.Interop.Excel;
using ESRI.ArcGIS.Server;
using System.Reflection;

public partial class PresentationLayer_ThongKeBaoCao2 : System.Web.UI.Page
{
    DataAccessHelper db = new DataAccessHelper();
    public int selectLayerIndex;
    List<ESRI.ArcGIS.Geodatabase.IFeatureClass> LfeatureClass= new List<ESRI.ArcGIS.Geodatabase.IFeatureClass>();

    OngPhanPhoiEntities ongPhanPhoiEntities = new OngPhanPhoiEntities();
    protected void Page_Load(object sender, EventArgs e)
    {      
        updateLayerList();
        if(!IsPostBack)
        updateFieldList();     
    }

    private void updateLayerList()
    {
         ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass;
            
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;

            try
            {
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
                // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();
                if (DListLop.SelectedIndex > 0)
                    selectLayerIndex = DListLop.SelectedIndex;
                DListLop.Items.Clear();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == "BTGISWASECO.SDE.wDataLogger")
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Name == "BTGISWASECO.SDE.WaterDistribution")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                          
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category != "BTGISWASECO.SDE.WaterDistribution")
                                {
                                    featureClass=featureWorkspace.OpenFeatureClass(_IDatasetName.Name);

                                    LfeatureClass.Add(featureClass);
                                    //LfeatureClass.Insert(k,featureWorkspace.OpenFeatureClass(_IDatasetName.Name));                             
                                    DListLop.Items.Add(new ListItem(featureClass.AliasName, _IDatasetName.Name));
                                
                                }
                                _IDatasetName = EnumDSName.Next();
                            }
                                if (selectLayerIndex < DListLop.Items.Count)
                                    DListLop.SelectedIndex = selectLayerIndex;

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
        }
    protected void DListLop_SelectedIndexChanged(object sender, EventArgs e)
    {
        updateFieldList();
    }

    private void updateFieldList()
    {
        ListBTruongLop.Items.Clear();
        ESRI.ArcGIS.Geodatabase.IFeatureClass iFrClass=null;
        foreach(var itemFclas in LfeatureClass)
        {
            if (DListLop.SelectedItem.Text == itemFclas.AliasName)
            {
                iFrClass = itemFclas;
                break;
            }
        }
        int t = iFrClass.Fields.FieldCount;
        for (int i = 0; i < iFrClass.Fields.FieldCount; i++)
        {
            if ((!iFrClass.Fields.get_Field(i).Name.Equals("SHAPE")) && (!iFrClass.Fields.get_Field(i).Name.Equals("OBJECTID")))
            {
                ListItem iteml = new ListItem(iFrClass.Fields.get_Field(i).AliasName,iFrClass.Fields.get_Field(i).Name);
                ListBTruongLop.Items.Add(iteml);
            }

            
        }
    
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ListBTruongLop.Items.Count > 0 && ListBTruongLop.SelectedIndex > -1)
            if (ListBTruongLop.SelectedValue.ToString() != "")
            {
                while (ListBTruongLop.Items.Count > 0 && ListBTruongLop.SelectedItem != null)
                {
                    ListItem selectedItem = ListBTruongLop.SelectedItem;
                    selectedItem.Selected = false;
                    ListBTruongTK.Items.Add(selectedItem);
                    ListBTruongLop.Items.Remove(selectedItem);
                }

            }       
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (ListBTruongTK.Items.Count > 0 && ListBTruongTK.SelectedIndex > -1)
            while (ListBTruongTK.Items.Count > 0 && ListBTruongTK.SelectedItem != null)
            {
                ListItem selectedItem = ListBTruongTK.SelectedItem;
                selectedItem.Selected = false;
                ListBTruongLop.Items.Add(selectedItem);
                ListBTruongTK.Items.Remove(selectedItem);
            }     
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        #region khai bao
        ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null;
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;
            string stXuatExcel = string.Empty;
            string stDXuatExcel = string.Empty;
            UtilitiesXuLyDomain uxlDM = new UtilitiesXuLyDomain();

            #region khai bao danh muc
            List<DMChieuDongVanEntities> LDMChieuDongVanEntities = new List<DMChieuDongVanEntities>();
            bool bDMChieuDongVanEntities = false;
            List<DMChucNangVanEntities> LDMChucNangVanEntities = new List<DMChucNangVanEntities>();
            bool bDMChucNangVanEntities = false;
            List<DMHieuDataLoggerEntities> LDMHieuDataLoggerEntities = new List<DMHieuDataLoggerEntities>();
            bool bDMHieuDataLoggerEntities = false;
            List<DMHieuMayBomEntities> LDMHieuMayBomEntities = new List<DMHieuMayBomEntities>();
            bool bDMHieuMayBomEntities = false;
            List<DMHieuTruCuuHoaEntities> LDMHieuTruCuuHoaEntities = new List<DMHieuTruCuuHoaEntities>();
            bool bDMHieuTruCuuHoaEntities = false;
            List<DMHieuVanEntities> LDMHieuVanEntities = new List<DMHieuVanEntities>();
            bool bDMHieuVanEntities = false;
            List<DMHuongDongChayEntities> LDMHuongDongChayEntities = new List<DMHuongDongChayEntities>();
            bool bDMHuongDongChayDAL = false;
            List<DMKichCoDongHoEntities> LDMKichCoDongHoEntities = new List<DMKichCoDongHoEntities>();
            bool bDMKichCoDongHoEntities = false;
            List<DMKichCoOngEntities> LDMKichCoOngEntities = new List<DMKichCoOngEntities>();
            bool bDMKichCoOngEntities = false;
            List<DMKichCoVanEntities> LDMKichCoVanEntities = new List<DMKichCoVanEntities>();
            bool bDMKichCoVanEntities = false;
            List<DMLCCVHDongHoEntities> LDMLCCVHDongHoEntities = new List<DMLCCVHDongHoEntities>();
            bool bDMLCCVHDongHoEntities = false;
            List<DMLoaiCauTaoDongHoEntities> LDMLoaiCauTaoDongHoEntities = new List<DMLoaiCauTaoDongHoEntities>();
            bool bDMLoaiCauTaoDongHoEntities = false;
            List<DMLoaiDonViEntities> LDMLoaiDonViEntities = new List<DMLoaiDonViEntities>();
            bool bDMLoaiDonViEntities = false;
            List<DMLoaiMatSoDongHoEntities> LDMLoaiMatSoDongHoEntities = new List<DMLoaiMatSoDongHoEntities>();
            bool bDMLoaiMatSoDongHoEntities = false;
            List<DMLoaiMayBomEntities> LDMLoaiMayBomEntities = new List<DMLoaiMayBomEntities>();
            bool bDMLoaiMayBomEntities = false;
            List<DMLoaiOngNoiEntities> LDMLoaiOngNoiEntities = new List<DMLoaiOngNoiEntities>();
            bool bDMLoaiOngNoiEntities = false;
            List<DMLoaiTramBomEntities> LDMLoaiTramBomEntities = new List<DMLoaiTramBomEntities>();
            bool bDMLoaiTramBomEntities = false;
            List<DMLoaiTruHongEntities> LDMLoaiTruHongEntities = new List<DMLoaiTruHongEntities>();
            bool bDMLoaiTruHongEntities = false;
            List<DMLoaiVanEntities> LDMLoaiVanEntities = new List<DMLoaiVanEntities>();
            bool bDMLoaiVanEntities = false;
            List<DMNuocSanXuatEntities> LDMNuocSanXuatEntities = new List<DMNuocSanXuatEntities>();
            bool bDMNuocSanXuatEntities = false;
            List<DMNhaSanXuatEntities> LDMNhaSanXuatEntities = new List<DMNhaSanXuatEntities>();
            bool bDMNhaSanXuatEntities = false;
            List<DMTieuChuanDongHoEntities> LDMTieuChuanDongHoEntities = new List<DMTieuChuanDongHoEntities>();
            bool bDMTieuChuanDongHoEntities = false;
            List<DMTieuChuanOngNuocEntities> LDMTieuChuanOngNuocEntities = new List<DMTieuChuanOngNuocEntities>();
            bool bDMTieuChuanOngNuocEntities = false;
            List<DMTieuChuanOngNoiEntities> LDMTieuChuanOngNoiEntities = new List<DMTieuChuanOngNoiEntities>();
            bool bDMTieuChuanOngNoiEntities = false;
            List<DMTieuChuanTruHongEntities> LDMTieuChuanTruHongEntities = new List<DMTieuChuanTruHongEntities>();
            bool bDMTieuChuanTruHongEntities = false;
            List<DMTieuChuanVanEntities> LDMTieuChuanVanEntities = new List<DMTieuChuanVanEntities>();
            bool bDMTieuChuanVanEntities = false;
            List<DMTinhTrangSDEntities> LDMTinhTrangSDEntities = new List<DMTinhTrangSDEntities>();
            bool bDMTinhTrangSDEntities = false;
            List<DMThuocSoHuuEntities> LDMThuocSoHuuEntities = new List<DMThuocSoHuuEntities>();
            bool bDMThuocSoHuuEntities = false;
            List<DMVatLieuEntities> LDMVatLieuEntities  = new List<DMVatLieuEntities>();
            bool bDMVatLieuEntities = false;           
          
            #endregion khai bao danh muc

        #endregion khai bao

            try
            {
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
                // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == DListLop.SelectedValue)
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Category == "SDE Feature Dataset")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category == "SDE Feature Class")
                                {
                                    if (_IDatasetName.Name == DListLop.SelectedValue)
                                    {
                                        featureClass = featureWorkspace.OpenFeatureClass(_IDatasetName.Name);
                                        break;
                                    }
                                }
                                _IDatasetName = EnumDSName.Next();
                            }

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }

                iFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayer();
                iFeatureLayer.FeatureClass = featureClass;
                //featureClass.Fields.Field.Domain.Name;
                ESRI.ArcGIS.Geodatabase.ITable tb = featureClass as ESRI.ArcGIS.Geodatabase.ITable;
                             
                ESRI.ArcGIS.Geodatabase.IFeature feature = null;
                try
                {
                    HttpContext context = HttpContext.Current;
                    context.Response.Clear(); // xóa rỗng khoảng nhớ tạm thời để ghi dữ liệu
                    context.Response.ContentEncoding = System.Text.Encoding.Unicode;
                    context.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
                    
                    ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    //queryFilter.WhereClause = "OBJECTID =10";
                    queryFilter.SubFields = "*";
                    ESRI.ArcGIS.Geodatabase.IRow row = null;
                    ESRI.ArcGIS.Geodatabase.ICursor cursor = tb.Search(queryFilter, true);
                   
                    for (int t = 0; t < ListBTruongTK.Items.Count;t++ )
                    {
                        context.Response.Write(ListBTruongTK.Items[t].Text + "\t");
                    }
                    context.Response.Write(Environment.NewLine);

                        while ((row = cursor.NextRow()) != null)
                        {
                            for (int k = 0; k < ListBTruongTK.Items.Count; k++)
                            {
                                //Tìm trường là Domain
                                for (int j = 0; j < cursor.Fields.FieldCount; j++)
                                    if (cursor.Fields.get_Field(j).Domain != null)
                                    {
                                        if (ListBTruongTK.Items[k].Text == cursor.Fields.get_Field(j).AliasName)
                                        {
                                          
                                            #region thu 
                                            switch (ListBTruongTK.Items[k].Text)
                                            {
                                                case "Hướng dòng chảy":
                                                    {
                                                        if (bDMHuongDongChayDAL == false)
                                                        {
                                                            LDMHuongDongChayEntities = DMHuongDongChayDAL.GetList();
                                                            bDMHuongDongChayDAL =true;
                                                        }
                                                        foreach (var itemc in LDMHuongDongChayEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Kích cở ống (mm)":
                                                    {
                                                        if (bDMKichCoOngEntities == false)
                                                        {
                                                            LDMKichCoOngEntities = DMKichCoOngDAL.GetList();
                                                            bDMKichCoOngEntities = true;
                                                        }
                                                        foreach (var itemc in LDMKichCoOngEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Kích cở":
                                                    {
                                                        if (bDMKichCoOngEntities == false)
                                                        {
                                                            LDMKichCoOngEntities = DMKichCoOngDAL.GetList();
                                                            bDMKichCoOngEntities = true;
                                                        }
                                                        foreach (var itemc in LDMKichCoOngEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Vật liệu":
                                                    {
                                                         if (bDMVatLieuEntities == false)
                                                        {
                                                            LDMVatLieuEntities = DMVatLieuDAL.GetList();
                                                            bDMVatLieuEntities = true;
                                                        }

                                                         foreach (var itemc in LDMVatLieuEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Tiêu chuẩn vật liệu":
                                                    {
                                                        if (bDMTieuChuanOngNuocEntities == false)
                                                        {
                                                            LDMTieuChuanOngNuocEntities = DMTieuChuanOngNuocDAL.GetList();
                                                            bDMTieuChuanOngNuocEntities = true;
                                                        }
                                                        foreach (var itemc in LDMTieuChuanOngNuocEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Tiêu chuẩn":
                                                    {
                                                        if (bDMTieuChuanOngNoiEntities == false)
                                                        {
                                                            LDMTieuChuanOngNoiEntities = DMTieuChuanOngNoiDAL.GetList();
                                                            bDMTieuChuanOngNoiEntities = true;
                                                        }
                                                        foreach (var itemc in LDMTieuChuanOngNoiEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Nhà sản xuất":
                                                    {
                                                        if (bDMNhaSanXuatEntities == false)
                                                        {
                                                            LDMNhaSanXuatEntities = DMNhaSanXuatDAL.GetList();
                                                            bDMNhaSanXuatEntities = true;
                                                        }
                                                        foreach (var itemc in LDMNhaSanXuatEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Nước sản xuất":
                                                    {
                                                        if (bDMNuocSanXuatEntities == false)
                                                        {
                                                            LDMNuocSanXuatEntities = DMNuocSanXuatDAL.GetList();
                                                            bDMNuocSanXuatEntities = true;
                                                        }
                                                        foreach (var itemc in LDMNuocSanXuatEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;

                                                case "Tình trạng":
                                                    {
                                                        if (bDMTinhTrangSDEntities == false)
                                                        {
                                                            LDMTinhTrangSDEntities = DMTinhTrangSDDAL.GetList();
                                                            bDMTinhTrangSDEntities = true;
                                                        }
                                                        foreach (var itemc in LDMTinhTrangSDEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Tình trạng máy bơm":
                                                    {
                                                        if (bDMTinhTrangSDEntities == false)
                                                        {
                                                            LDMTinhTrangSDEntities = DMTinhTrangSDDAL.GetList();
                                                            bDMTinhTrangSDEntities = true;
                                                        }
                                                        foreach (var itemc in LDMTinhTrangSDEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Tình trạng biến tần":
                                                    {
                                                        if (bDMTinhTrangSDEntities == false)
                                                        {
                                                            LDMTinhTrangSDEntities = DMTinhTrangSDDAL.GetList();
                                                            bDMTinhTrangSDEntities = true;
                                                        }
                                                        foreach (var itemc in LDMTinhTrangSDEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Đơn vị thiết kế":
                                                    {
                                                        if (bDMLoaiDonViEntities == false)
                                                        {
                                                            LDMLoaiDonViEntities = DMLoaiDonViDAL.GetList();
                                                            bDMLoaiDonViEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiDonViEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Đơn vị thi công":
                                                    {
                                                        if (bDMLoaiDonViEntities == false)
                                                        {
                                                            LDMLoaiDonViEntities = DMLoaiDonViDAL.GetList();
                                                            bDMLoaiDonViEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiDonViEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case "Đơn vị giám sát":
                                                    {
                                                        if (bDMLoaiDonViEntities == false)
                                                        {
                                                            LDMLoaiDonViEntities = DMLoaiDonViDAL.GetList();
                                                            bDMLoaiDonViEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiDonViEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                
                                                    case "Nhãn hiệu":
                                                    {
                                                        if (bDMHieuDataLoggerEntities == false)
                                                        {
                                                            LDMHieuDataLoggerEntities = DMHieuDataLoggerDAL.GetList();
                                                            bDMHieuDataLoggerEntities = true;
                                                        }
                                                        foreach (var itemc in LDMHieuDataLoggerEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Thuộc sở hữu":
                                                    {
                                                        if (bDMThuocSoHuuEntities == false)
                                                        {
                                                            LDMThuocSoHuuEntities = DMThuocSoHuuDAL.GetList();
                                                            bDMThuocSoHuuEntities = true;
                                                        }
                                                        foreach (var itemc in LDMThuocSoHuuEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại cấu tạo":
                                                    {
                                                        if (bDMLoaiCauTaoDongHoEntities == false)
                                                        {
                                                            LDMLoaiCauTaoDongHoEntities = DMLoaiCauTaoDongHoDAL.GetList();
                                                            bDMLoaiCauTaoDongHoEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiCauTaoDongHoEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại cấu cách vận hành":
                                                    {
                                                        if (bDMLCCVHDongHoEntities == false)
                                                        {
                                                            LDMLCCVHDongHoEntities = DMLCCVHDongHoDAL.GetList();
                                                            bDMLCCVHDongHoEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLCCVHDongHoEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại mặt số":
                                                    {
                                                        if (bDMLoaiMatSoDongHoEntities == false)
                                                        {
                                                            LDMLoaiMatSoDongHoEntities = DMLoaiMatSoDongHoDAL.GetList();
                                                            bDMLoaiMatSoDongHoEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiMatSoDongHoEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại ống nối":
                                                    {
                                                        if (bDMLoaiOngNoiEntities == false)
                                                        {
                                                            LDMLoaiOngNoiEntities = DMLoaiOngNoiDAL.GetList();
                                                            bDMLoaiOngNoiEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiOngNoiEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Tiêu chuẩn đồng hồ":
                                                    {
                                                        if (bDMTieuChuanDongHoEntities == false)
                                                        {
                                                            LDMTieuChuanDongHoEntities = DMTieuChuanDongHoDAL.GetList();
                                                            bDMTieuChuanDongHoEntities = true;
                                                        }
                                                        foreach (var itemc in LDMTieuChuanDongHoEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại cấu tạo đồng hồ":
                                                    {
                                                        if (bDMLoaiCauTaoDongHoEntities == false)
                                                        {
                                                            LDMLoaiCauTaoDongHoEntities = DMLoaiCauTaoDongHoDAL.GetList();
                                                            bDMLoaiCauTaoDongHoEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiCauTaoDongHoEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Kích cở đồng hồ":
                                                    {
                                                        if (bDMKichCoDongHoEntities == false)
                                                        {
                                                            LDMKichCoDongHoEntities = DMKichCoDongHoDAL.GetList();
                                                            bDMKichCoDongHoEntities = true;
                                                        }
                                                        foreach (var itemc in LDMKichCoDongHoEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Nhãn hiệu máy bơm":
                                                    {
                                                        if (bDMHieuMayBomEntities == false)
                                                        {
                                                            LDMHieuMayBomEntities = DMHieuMayBomDAL.GetList();
                                                            bDMHieuMayBomEntities = true;
                                                        }
                                                        foreach (var itemc in LDMHieuMayBomEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Kiểu máy bơm":
                                                    {
                                                        if (bDMLoaiMayBomEntities == false)
                                                        {
                                                            LDMLoaiMayBomEntities = DMLoaiMayBomDAL.GetList();
                                                            bDMLoaiMayBomEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiMayBomEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại trạm":
                                                    {
                                                        if (bDMLoaiTramBomEntities == false)
                                                        {
                                                            LDMLoaiTramBomEntities = DMLoaiTramBomDAL.GetList();
                                                            bDMLoaiTramBomEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiTramBomEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Loại trụ họng":
                                                    {
                                                        if (bDMLoaiTruHongEntities == false)
                                                        {
                                                            LDMLoaiTruHongEntities = DMLoaiTruHongDAL.GetList();
                                                            bDMLoaiTruHongEntities = true;
                                                        }
                                                        foreach (var itemc in LDMLoaiTruHongEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Chiều đóng van":
                                                    {
                                                        if (bDMChieuDongVanEntities == false)
                                                        {
                                                            LDMChieuDongVanEntities = DMChieuDongVanDAL.GetList();
                                                            bDMChieuDongVanEntities = true;
                                                        }
                                                        foreach (var itemc in LDMChieuDongVanEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                    case "Chức năng van":
                                                    {
                                                        if (bDMChucNangVanEntities == false)
                                                        {
                                                            LDMChucNangVanEntities = DMChucNangVanDAL.GetList();
                                                            bDMChucNangVanEntities = true;
                                                        }
                                                        foreach (var itemc in LDMChucNangVanEntities)
                                                        {
                                                            if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                                            {
                                                                stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                default:
                                                   // stDXuatExcel = stDXuatExcel+"\t";
                                                    break;
                                            }
                                            #endregion thu
                                            //foreach (var itemc in DMHuongDongChayDAL.GetList())
                                            //{
                                            //    if (row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() == itemc.Ma.ToString().Trim())
                                            //    {
                                            //        stDXuatExcel = stDXuatExcel + itemc.MoTa.ToString() + "\t";
                                            //    }
                                            //}
                                            //LStMota = null;
                                        }
                                    }
                                // Ket thuc Tìm trường là Domain
                                if (stDXuatExcel==string.Empty)
                                    stDXuatExcel = stDXuatExcel + row.get_Value(cursor.FindField(ListBTruongTK.Items[k].Value)).ToString() + "\t";
                                stXuatExcel = stXuatExcel + stDXuatExcel;
                                stDXuatExcel = string.Empty;
                            }
                            context.Response.Write(stXuatExcel);
                            stXuatExcel = string.Empty;
                            context.Response.Write(Environment.NewLine);
                        }

                        context.Response.AddHeader("content-disposition", "attachment; filename=" + "Report Online GIS" + ".xls");
                        context.Response.ContentType = "application/vnd.ms-excel";
                        context.Response.End();
                }
                catch (System.Runtime.InteropServices.COMException comExc)
                {
                }

               // System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);


                //foreach (DataColumn column in table.Columns)
                //{
                //    context.Response.Write(column.ColumnName + ";");
                //}
                //context.Response.Write(Environment.NewLine);
                //foreach (DataRow row in table.Rows)
                //{
                //    for (int i = 0; i < table.Columns.Count; i++)
                //    {
                //        context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
                //    }
                //    context.Response.Write(Environment.NewLine);
                //}

            }
          
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
    }

 

    public System.Data.DataTable ConvertITable(ESRI.ArcGIS.Geodatabase.ITable table)
    {

        System.Data.DataTable ExpertInfo;
        ExpertInfo = new System.Data.DataTable("ExpertInfo");

        try
        {
            ESRI.ArcGIS.Geodatabase.IQueryFilter que = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
            ESRI.ArcGIS.Geodatabase.ICursor pCursor = table.Search(null, false);
            ESRI.ArcGIS.Geodatabase.IRow pRow = pCursor.NextRow();

            if (pRow != null)
            {
                for (int i = 0; i < pRow.Fields.FieldCount; i++)
                {
                    ExpertInfo.Columns.Add(pRow.Fields.get_Field(i).Name);
                }
                while (pRow != null)
                {
                    DataRow pDataRow = ExpertInfo.NewRow();
                    for (int j = 0; j < pCursor.Fields.FieldCount; j++)
                        pDataRow[j] = pRow.get_Value(j);
                    ExpertInfo.Rows.Add(pDataRow);
                    pRow = pCursor.NextRow();
                }
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }

        return ExpertInfo;



    }
}